import datetime
# from main import MainWindow
from PySide6.QtGui import Qt, QPixmap
from PySide6.QtWidgets import *
from mysql import MyMysql


class Login(QMainWindow):
    def __init__(self):
        super().__init__()
        self.main_window = None
        self.mysql = MyMysql()
        self.resize(600, 600)
        self.setContentsMargins(100, 200, 100, 100)
        self.setStyleSheet("QMainWindow {"
                           "background-image: url(1.png);"
                           "background-position: center;"
                           "background-repeat: no-repeat;"
                           "}")


        stacked_widget = QStackedWidget()

        # 登录界面
        logo_label = QLabel()
        pixmap = QPixmap("4.png")  # 请将logo.png替换为您自己的图片路径
        logo_label.setPixmap(pixmap)
        logo_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.input_name = QLineEdit()
        self.input_name.setPlaceholderText("名字")
        self.input_name.setFixedHeight(40)
        self.input_name.setFixedWidth(250)
        self.input_pwd = QLineEdit()
        self.input_pwd.setPlaceholderText("密码")
        self.input_pwd.setEchoMode(QLineEdit.Password)
        self.input_pwd.setFixedHeight(40)
        self.input_pwd.setFixedWidth(250)
        but_login = QPushButton("登录")
        but_login.setFixedHeight(30)
        but_login.setFixedWidth(250)
        but_login.setStyleSheet("background-color:greencolor:black")
        but_login.clicked.connect(self.login)
        regi = QLabel("新用户注册")
        regi.setFixedSize(70, 18)
        regi.mousePressEvent = lambda event: stacked_widget.setCurrentWidget(self.regi_background)

        self.login_background = QWidget()
        self.login_background.setFixedSize(270, 300)
        self.login_background.setStyleSheet("background-color: rgba(255, 255, 255, 180);color:black")

        self.layout_login = QVBoxLayout()
        self.layout_login.addWidget(logo_label)
        self.layout_login.addWidget(self.input_name)
        self.layout_login.addWidget(self.input_pwd)
        self.layout_login.addWidget(but_login)
        self.layout_login.addWidget(regi)
        self.login_background.setLayout(self.layout_login)

        # 注册界面
        top_label = QLabel("手机号注册")
        top_label.setStyleSheet("font-size: 20px;")
        self.input_name = QLineEdit()
        self.input_name.setPlaceholderText("名字")
        self.input_name.setFixedHeight(40)
        self.input_name.setFixedWidth(250)
        self.input_phone = QLineEdit()
        self.input_phone.setPlaceholderText("手机号")
        self.input_phone.setFixedHeight(40)
        self.input_phone.setFixedWidth(250)
        self.input_card = QLineEdit()
        self.input_card.setPlaceholderText("卡号")
        self.input_card.setFixedHeight(40)
        self.input_card.setFixedWidth(250)
        self.input_pwd = QLineEdit()
        self.input_pwd.setPlaceholderText("密码")
        self.input_pwd.setEchoMode(QLineEdit.Password)
        self.input_pwd.setFixedHeight(40)
        self.input_pwd.setFixedWidth(250)
        but_regi = QPushButton("注册")
        but_regi.setFixedHeight(30)
        but_regi.setFixedWidth(250)
        but_regi.setStyleSheet("background-color:green;color:black")
        but_regi.clicked.connect(self.regi)
        sign = QLabel("登录")
        sign.setFixedSize(28, 18)
        sign.mousePressEvent = lambda event: stacked_widget.setCurrentWidget(self.login_background)

        self.regi_background = QWidget()
        self.regi_background.setFixedSize(270, 300)
        self.regi_background.setStyleSheet("background-color: rgba(255, 255, 255, 180);color:black")

        self.layout_regi = QVBoxLayout()
        self.layout_regi.addWidget(top_label)
        self.layout_regi.addWidget(self.input_name)
        self.layout_regi.addWidget(self.input_phone)
        self.layout_regi.addWidget(self.input_card)
        self.layout_regi.addWidget(self.input_pwd)
        self.layout_regi.addWidget(but_regi)
        self.layout_regi.addWidget(sign)
        self.regi_background.setLayout(self.layout_regi)

        stacked_widget.addWidget(self.login_background)
        stacked_widget.addWidget(self.regi_background)

        self.setCentralWidget(stacked_widget)

    def login(self):
        name = self.input_name.text()
        rest = self.mysql.select_all_user(name)
        if rest:
            if self.input_pwd.text() != rest[7]:
                QMessageBox.warning(self, "登录结果", "登录失败")
            else:
                if rest[3] == 0:
                    QMessageBox.information(self, "登录结果", "登录成功")
                    if not self.main_window:  # 如果注册界面实例不存在，则创建并显示
                        self.main_window = MainWindow()
                        self.main_window.show()
                        self.hide()
                else:
                    QMessageBox.warning(self, "登录结果", "你不是管理员，请退出")
        else:
            QMessageBox.warning(self, "登录结果", "没有此用户，请先注册")

    def regi(self):
        current_time = datetime.datetime.now()
        self.mysql.insert_one_user(
            [self.input_name.text(), self.input_phone.text(), 0, current_time, self.input_card.text(),
             self.input_pwd.text()])
        QMessageBox.information(self, "注册结果", "注册成功")


if __name__ == '__main__':
    app = QApplication([])
    login_page = Login()
    login_page.show()
    app.exec()
